package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.dababases.dao.ItemDao;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDao;
import com.scau.yangsuiyu.estore.dababases.dao.OrderDetailDao;
import com.scau.yangsuiyu.estore.dababases.entity.*;
import com.scau.yangsuiyu.estore.util.ImageViewUtil;
import com.scau.yangsuiyu.estore.util.OrderStatus;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 买家的订单HBox
 * @date 2019-06-12 19:57
 * @email suiyu_yang@163.com
 */
public class BuyerOrderHBox extends HBox implements Initializable {
    private Orders orders;

    @FXML
    private Label orderTimeLabel;

    @FXML
    private Label orderIdLabel;


    @FXML
    private Label goodsNameLabel;

    @FXML
    private ImageView goodsPhoto;

    @FXML
    private Label totalPriceLabel;

    @FXML
    private Label promotionalPriceLabel;

    @FXML
    private Label amountLabel;

    @FXML
    private Label checkDetailLabel;

    @FXML
    private Label orderStatusLabel;

    @FXML
    private Button changeStatusButton;

    @FXML
    private Label gotoCommentLabel;

    public BuyerOrderHBox(Orders orders) {
        this.orders = orders;
        loadFXML();
    }


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        changeStatusButton.disableProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (newValue) { //最新的button被禁用
                    changeStatusButton.setText("已收货");
                } else {
                    changeStatusButton.setText("确认收货");
                }
            }
        });
        if (orders.getStatus().equals(OrderStatus.UN_RECEIVED)) {
            changeStatusButton.setDisable(false);
            changeStatusButton.setVisible(true);
        } else {
            changeStatusButton.setDisable(true);
            changeStatusButton.setVisible(false);
        }
        setOrdersToUI();
    }

    private void setOrdersToUI() {
        List<OrderDetail> orderDetailList = new ArrayList<>(new OrderDetailDao().getOrderDetailByOrderId(orders.getId()));
        Item firstItem = new ItemDao().getItemByItemId(orderDetailList.get(0).getItemId());
        Goods firstGoods = firstItem.getGoods();
        List<GoodsPhoto> goodsPhotoList = new ArrayList<>(firstGoods.getGoodsPhotoList());

        orderTimeLabel.setText(orders.getCreateTime().toLocalDateTime().toString());
        orderIdLabel.setText(orders.getId() + "");
        goodsNameLabel.setText(firstGoods.getGoodsName());
        ImageViewUtil.loadImage(goodsPhoto, goodsPhotoList.get(0).getPhotoUrl());
        totalPriceLabel.setText("¥" + orders.getTotalPrice());
        promotionalPriceLabel.setText("¥" + orders.getPaymentPrice());
        orderStatusLabel.setText(OrderStatus.statusMap.get(orders.getStatus()));

    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/BuyerOrderHBox.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @FXML
    private void changeStatusClick() {
        orders.setStatus(OrderStatus.UN_REVIEWED);
        new OrderDao().update(orders);
        setOrdersToUI();
        changeStatusButton.setText("已收货");
        changeStatusButton.setDisable(true);
        orderStatusLabel.setText("待评价");
        gotoCommentLabel.setVisible(true);
    }

    @FXML
    private void showOrdersDetail() {
        new OrderDetailPane(orders).showPane();
    }
}
